package com.asen.commons.utils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class JsonUtils {
    public static final String DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final TimeZone defaultTimeZone = TimeZone.getDefault();
    public static final Locale defaultLocale = Locale.getDefault();

    public JsonUtils() {
    }

    public static Boolean castToBoolean(Object value) {
        if (value == null) {
            return null;
        } else if (value instanceof Boolean) {
            return (Boolean)value;
        } else if (value instanceof Number) {
            return ((Number)value).intValue() == 1;
        } else {
            if (value instanceof String) {
                String strVal = (String)value;
                if (strVal.length() == 0 || "null".equals(strVal) || "NULL".equals(strVal)) {
                    return null;
                }

                if ("true".equalsIgnoreCase(strVal) || "1".equals(strVal)) {
                    return Boolean.TRUE;
                }

                if ("false".equalsIgnoreCase(strVal) || "0".equals(strVal)) {
                    return Boolean.FALSE;
                }

                if ("Y".equalsIgnoreCase(strVal) || "T".equals(strVal)) {
                    return Boolean.TRUE;
                }

                if ("F".equalsIgnoreCase(strVal) || "N".equals(strVal)) {
                    return Boolean.FALSE;
                }
            }

            throw new IllegalArgumentException("can not cast to boolean, value : " + value);
        }
    }

    public static Date castToDate(Object value) {
        if (value == null) {
            return null;
        } else if (value instanceof Date) {
            return (Date)value;
        } else if (value instanceof Calendar) {
            return ((Calendar)value).getTime();
        } else {
            long longValue = -1L;
            if (value instanceof Number) {
                longValue = ((Number)value).longValue();
                return new Date(longValue);
            } else {
                if (value instanceof String) {
                    String strVal = (String)value;
                    if (strVal.startsWith("/Date(") && strVal.endsWith(")/")) {
                        strVal = strVal.substring(6, strVal.length() - 2);
                    }

                    if (strVal.indexOf(45) != -1) {
                        String format;
                        if (strVal.length() != "yyyy-MM-dd HH:mm:ss".length() && (strVal.length() != 22 || !"yyyy-MM-dd HH:mm:ss".equals("yyyyMMddHHmmssSSSZ"))) {
                            if (strVal.length() == 10) {
                                format = "yyyy-MM-dd";
                            } else if (strVal.length() == "yyyy-MM-dd HH:mm:ss".length()) {
                                format = "yyyy-MM-dd HH:mm:ss";
                            } else if (strVal.length() == 29 && strVal.charAt(26) == ':' && strVal.charAt(28) == '0') {
                                format = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
                            } else {
                                format = "yyyy-MM-dd HH:mm:ss.SSS";
                            }
                        } else {
                            format = "yyyy-MM-dd HH:mm:ss";
                        }

                        SimpleDateFormat dateFormat = new SimpleDateFormat(format, defaultLocale);
                        dateFormat.setTimeZone(defaultTimeZone);

                        try {
                            return dateFormat.parse(strVal);
                        } catch (ParseException var7) {
                            throw new IllegalArgumentException("can not cast to Date, value : " + strVal);
                        }
                    }

                    if (strVal.length() == 0) {
                        return null;
                    }

                    longValue = Long.parseLong(strVal);
                }

                if (longValue < 0L) {
                    throw new IllegalArgumentException("can not cast to Date, value : " + value);
                } else {
                    return new Date(longValue);
                }
            }
        }
    }

    public static Integer castToInt(Object value) {
        if (value == null) {
            return null;
        } else if (value instanceof Integer) {
            return (Integer)value;
        } else if (value instanceof Number) {
            return ((Number)value).intValue();
        } else if (value instanceof String) {
            String strVal = (String)value;
            if (strVal.length() != 0 && !"null".equals(strVal) && !"NULL".equals(strVal)) {
                if (strVal.indexOf(44) != 0) {
                    strVal = strVal.replaceAll(",", "");
                }

                return Integer.parseInt(strVal);
            } else {
                return null;
            }
        } else if (value instanceof Boolean) {
            return (Boolean)value ? 1 : 0;
        } else {
            if (value instanceof Map) {
                Map map = (Map)value;
                if (map.size() == 2 && map.containsKey("andIncrement") && map.containsKey("andDecrement")) {
                    Iterator iter = map.values().iterator();
                    iter.next();
                    Object value2 = iter.next();
                    return castToInt(value2);
                }
            }

            throw new IllegalArgumentException("can not cast to int, value : " + value);
        }
    }

    public static Long castToLong(Object value) {
        if (value == null) {
            return null;
        } else if (value instanceof Number) {
            return ((Number)value).longValue();
        } else if (value instanceof String) {
            String strVal = (String)value;
            if (strVal.length() != 0 && !"null".equals(strVal) && !"NULL".equals(strVal)) {
                if (strVal.indexOf(44) != 0) {
                    strVal = strVal.replaceAll(",", "");
                }

                return Long.parseLong(strVal);
            } else {
                return null;
            }
        } else {
            if (value instanceof Map) {
                Map map = (Map)value;
                if (map.size() == 2 && map.containsKey("andIncrement") && map.containsKey("andDecrement")) {
                    Iterator iter = map.values().iterator();
                    iter.next();
                    Object value2 = iter.next();
                    return castToLong(value2);
                }
            }

            throw new IllegalArgumentException("can not cast to long, value : " + value);
        }
    }

    public static BigDecimal castToBigDecimal(Object value) {
        if (value == null) {
            return null;
        } else if (value instanceof BigDecimal) {
            return (BigDecimal)value;
        } else if (value instanceof BigInteger) {
            return new BigDecimal((BigInteger)value);
        } else {
            String strVal = value.toString();
            if (strVal.length() == 0) {
                return null;
            } else {
                return value instanceof Map && ((Map)value).size() == 0 ? null : new BigDecimal(strVal);
            }
        }
    }
}
